<?php
/**
 * @author Mazvv
 * @package Employeers
 */
class Employeers extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'employeers';
		$this->lang->load('employeers');
		$this->load->model('employeers_model');
		$this->load->model('filials_model');
		
		$this->a_validation_rules['_filials_rid'] = "required";       
		$this->a_validation_rules['f_name'] = "required";         
		$this->a_validation_rules['s_name'] = "required";         
		$this->a_validation_rules['l_name'] = "required";        
		//$this->a_validation_rules['f_name_lat'] = "required";         
		//$this->a_validation_rules['l_name_lat'] = "required";         
		$this->a_validation_rules['birthday'] = "required";         
		$this->a_validation_rules['bdate'] = "required";         
		//$this->a_validation_rules['edate'] = "required";         
		//$this->a_validation_rules['passp_seria'] = "required";         
		//$this->a_validation_rules['passp_num'] = "required";
		//$this->a_validation_rules['fpassp_seria'] = "required";         
		//$this->a_validation_rules['fpassp_num'] = "required";         
		//$this->a_validation_rules['nal_number'] = "required";
		//$this->a_validation_rules['stazh'] = "required";         		
		$this->a_validation_rules['email'] = "required";
		
		$this->a_validation_fields['_filials_rid'] = $this->lang->line('FILIAL');
		$this->a_validation_fields['f_name'] = $this->lang->line('F_NAME');  
		$this->a_validation_fields['s_name'] = $this->lang->line('S_NAME');  
		$this->a_validation_fields['l_name'] = $this->lang->line('L_NAME'); 
		$this->a_validation_fields['f_name_lat'] = $this->lang->line('F_NAME_LAT');  
		$this->a_validation_fields['l_name_lat'] = $this->lang->line('L_NAME_LAT');  
		$this->a_validation_fields['birthday'] = $this->lang->line('BIRTHDAY');  
		$this->a_validation_fields['bdate'] = $this->lang->line('BDATE');  
		$this->a_validation_fields['edate'] = $this->lang->line('EDATE');  
		$this->a_validation_fields['passp_seria'] = $this->lang->line('PASSP_SERIA');  
		$this->a_validation_fields['passp_num'] = $this->lang->line('PASSP_NUM');
		$this->a_validation_fields['fpassp_seria'] = $this->lang->line('FPASSP_SERIA');  
		$this->a_validation_fields['fpassp_num'] = $this->lang->line('FPASSP_NUM');  
		$this->a_validation_fields['stazh'] = $this->lang->line('STAZH');  
		$this->a_validation_fields['email'] = $this->lang->line('EMAIL');  
		
		$this->a_validation_fields['nal_number'] = $this->lang->line('NAL_NUM'); 		
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		$content['filial_name'] = $this->lang->line('FILIAL');
		$content['f_name'] = $this->lang->line('F_NAME');  
		$content['s_name'] = $this->lang->line('S_NAME');  
		$content['l_name'] = $this->lang->line('L_NAME'); 
		$content['f_name_lat'] = $this->lang->line('F_NAME_LAT');  
		$content['l_name_lat'] = $this->lang->line('L_NAME_LAT');  
		$content['birthday'] = $this->lang->line('BIRTHDAY');  
		$content['bdate'] = $this->lang->line('BDATE');  
		$content['edate'] = $this->lang->line('EDATE');  
		$content['passp_seria'] = $this->lang->line('PASSP_SERIA');  
		$content['passp_num'] = $this->lang->line('PASSP_NUM');  
		$content['nal_number'] = $this->lang->line('NAL_NUM'); 		
		
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->employeers_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if(!$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('employeers/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			# { _filials_rid
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $this->filials_model->GetPairArr(), $this->validation->_filials_rid?$this->validation->_filials_rid:'');
			# } _filials_rid
			
			# { f_name
			$content['f_name'] = $this->RenderLabel('f_name');
			$content['f_name_field'] =  form_input('f_name', $this->validation->f_name?$this->validation->f_name:'');
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->RenderLabel('s_name');
			$content['s_name_field'] =  form_input('s_name', $this->validation->s_name?$this->validation->s_name:'');
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->RenderLabel('l_name');
			$content['l_name_field'] =  form_input('l_name', $this->validation->l_name?$this->validation->l_name:'');
			# } l_name
			
			# { f_name_lat
			$content['f_name_lat'] = $this->RenderLabel('f_name_lat');
			$content['f_name_lat_field'] =  form_input('f_name_lat', $this->validation->f_name_lat?$this->validation->f_name_lat:'');
			# } f_name_lat
			
			# { l_name_lat
			$content['l_name_lat'] = $this->RenderLabel('l_name_lat');
			$content['l_name_lat_field'] =  form_input('l_name_lat', $this->validation->l_name_lat?$this->validation->l_name_lat:'');
			# } l_name_lat
			
			# { birthday
			$content['birthday'] = $this->RenderLabel('birthday');
			$data = array('name'=>'birthday', 'id'=>'birthday', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->birthday?$this->validation->birthday:'');
			$content['birthday_field'] =  form_input($data).$this->RenderCalendar('birthday');
			# } birthday
			
			# { bdate
			$content['bdate'] = $this->RenderLabel('bdate');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->bdate?$this->validation->bdate:'');
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { edate
			$content['edate'] = $this->RenderLabel('edate');
			$data = array('name'=>'edate', 'id'=>'edate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->edate?$this->validation->edate:'');
			$content['edate_field'] =  form_input($data).$this->RenderCalendar('edate');
			# } edate
			
			# { passp_seria
			$content['passp_seria'] = $this->RenderLabel('passp_seria');
			$content['passp_seria_field'] =  form_input('passp_seria', $this->validation->passp_seria?$this->validation->passp_seria:'');
			# } passp_seria
			
			# { passp_num
			$content['passp_num'] = $this->RenderLabel('passp_num');
			$content['passp_num_field'] =  form_input('passp_num', $this->validation->passp_num?$this->validation->passp_num:'');
			# } passp_num
			
			# { fpassp_seria
			$content['fpassp_seria'] = $this->RenderLabel('fpassp_seria');
			$content['fpassp_seria_field'] =  form_input('fpassp_seria', $this->validation->fpassp_seria?$this->validation->fpassp_seria:'');
			# } fpassp_seria
			
			# { fpassp_num
			$content['fpassp_num'] = $this->RenderLabel('fpassp_num');
			$content['fpassp_num_field'] =  form_input('fpassp_num', $this->validation->fpassp_num?$this->validation->fpassp_num:'');
			# } fpassp_num
			
			# { stazh
			$content['stazh'] = $this->RenderLabel('stazh');
			$content['stazh_field'] =  form_input('stazh', $this->validation->stazh?$this->validation->stazh:'');
			# } stazh
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$content['email_field'] =  form_input('email', $this->validation->email?$this->validation->email:'');
			# } email
			
			# { nal_number
			$content['nal_number'] = $this->RenderLabel('nal_number');
			$content['nal_number_field'] =  form_input('nal_number', $this->validation->nal_number?$this->validation->nal_number:'');
			# } nal_number
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('employeers/create', $content, True);
		}
		if($insRid = $this->employeers_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->employeers_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			# { _filials_rid
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $this->filials_model->GetPairArr(), $this->validation->_filials_rid?$this->validation->_filials_rid:$content['DS']->_filials_rid);
			# } _filials_rid
			
			# { f_name
			$content['f_name'] = $this->RenderLabel('f_name');
			$content['f_name_field'] =  form_input('f_name', $this->validation->f_name?$this->validation->f_name:$content['DS']->f_name);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->RenderLabel('s_name');
			$content['s_name_field'] =  form_input('s_name', $this->validation->s_name?$this->validation->s_name:$content['DS']->s_name);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->RenderLabel('l_name');
			$content['l_name_field'] =  form_input('l_name', $this->validation->l_name?$this->validation->l_name:$content['DS']->l_name);
			# } l_name
			
			# { f_name_lat
			$content['f_name_lat'] = $this->RenderLabel('f_name_lat');
			$content['f_name_lat_field'] =  form_input('f_name_lat', $this->validation->f_name_lat?$this->validation->f_name_lat:$content['DS']->f_name_lat);
			# } f_name_lat
			
			# { l_name_lat
			$content['l_name_lat'] = $this->RenderLabel('l_name_lat');
			$content['l_name_lat_field'] =  form_input('l_name_lat', $this->validation->l_name_lat?$this->validation->l_name_lat:$content['DS']->l_name_lat);
			# } l_name_lat
			
			# { birthday
			$content['birthday'] = $this->RenderLabel('birthday');
			$data = array('name'=>'birthday', 'id'=>'birthday', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->birthday?$this->validation->birthday:$content['DS']->birthday);
			$content['birthday_field'] =  form_input($data).$this->RenderCalendar('birthday');
			# } birthday
			
			# { bdate
			$content['bdate'] = $this->RenderLabel('bdate');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->bdate?$this->validation->bdate:$content['DS']->bdate);
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { edate
			$content['edate'] = $this->RenderLabel('edate');
			$data = array('name'=>'edate', 'id'=>'edate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->edate?$this->validation->edate:$content['DS']->edate);
			$content['edate_field'] =  form_input($data).$this->RenderCalendar('edate');
			# } edate
			
			# { passp_seria
			$content['passp_seria'] = $this->RenderLabel('passp_seria');
			$content['passp_seria_field'] =  form_input('passp_seria', $this->validation->passp_seria?$this->validation->passp_seria:$content['DS']->passp_seria);
			# } passp_seria
			
			# { passp_num
			$content['passp_num'] = $this->RenderLabel('passp_num');
			$content['passp_num_field'] =  form_input('passp_num', $this->validation->passp_num?$this->validation->passp_num:$content['DS']->passp_num);
			# } passp_num
			
			# { fpassp_seria
			$content['fpassp_seria'] = $this->RenderLabel('fpassp_seria');
			$content['fpassp_seria_field'] =  form_input('fpassp_seria', $this->validation->fpassp_seria?$this->validation->fpassp_seria:$content['DS']->fpassp_seria);
			# } fpassp_seria
			
			# { fpassp_num
			$content['fpassp_num'] = $this->RenderLabel('fpassp_num');
			$content['fpassp_num_field'] =  form_input('fpassp_num', $this->validation->fpassp_num?$this->validation->fpassp_num:$content['DS']->fpassp_num);
			# } fpassp_num
			
			# { stazh
			$content['stazh'] = $this->RenderLabel('stazh');
			$content['stazh_field'] =  form_input('stazh', $this->validation->stazh?$this->validation->stazh:$content['DS']->stazh);
			# } stazh
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$content['email_field'] =  form_input('email', $this->validation->email?$this->validation->email:$content['DS']->email);
			# } email
			
			# { nal_number
			$content['nal_number'] = $this->RenderLabel('nal_number');
			$content['nal_number_field'] =  form_input('nal_number', $this->validation->nal_number?$this->validation->nal_number:$content['DS']->nal_number);
			# } nal_number
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('employeers/edit', $content, True);
		}
		if($this->employeers_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->employeers_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			# { _filials_rid
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $this->filials_model->GetPairArr(), $this->validation->_filials_rid?$this->validation->_filials_rid:$content['DS']->_filials_rid);
			# } _filials_rid
			
			# { f_name
			$content['f_name'] = $this->RenderLabel('f_name');
			$content['f_name_field'] =  form_input('f_name', $this->validation->f_name?$this->validation->f_name:$content['DS']->f_name);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->RenderLabel('s_name');
			$content['s_name_field'] =  form_input('s_name', $this->validation->s_name?$this->validation->s_name:$content['DS']->s_name);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->RenderLabel('l_name');
			$content['l_name_field'] =  form_input('l_name', $this->validation->l_name?$this->validation->l_name:$content['DS']->l_name);
			# } l_name
			
			# { f_name_lat
			$content['f_name_lat'] = $this->RenderLabel('f_name_lat');
			$content['f_name_lat_field'] =  form_input('f_name_lat', $this->validation->f_name_lat?$this->validation->f_name_lat:$content['DS']->f_name_lat);
			# } f_name_lat
			
			# { l_name_lat
			$content['l_name_lat'] = $this->RenderLabel('l_name_lat');
			$content['l_name_lat_field'] =  form_input('l_name_lat', $this->validation->l_name_lat?$this->validation->l_name_lat:$content['DS']->l_name_lat);
			# } l_name_lat
			
			# { birthday
			$content['birthday'] = $this->RenderLabel('birthday');
			$data = array('name'=>'birthday', 'id'=>'birthday', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->birthday?$this->validation->birthday:$content['DS']->birthday);
			$content['birthday_field'] =  form_input($data).$this->RenderCalendar('birthday');
			# } birthday
			
			# { bdate
			$content['bdate'] = $this->RenderLabel('bdate');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->bdate?$this->validation->bdate:$content['DS']->bdate);
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { edate
			$content['edate'] = $this->RenderLabel('edate');
			$data = array('name'=>'edate', 'id'=>'edate', 'readonly'=>'readonly', 'size'=>'12', 'value'=>$this->validation->edate?$this->validation->edate:$content['DS']->edate);
			$content['edate_field'] =  form_input($data).$this->RenderCalendar('edate');
			# } edate
			
			# { passp_seria
			$content['passp_seria'] = $this->RenderLabel('passp_seria');
			$content['passp_seria_field'] =  form_input('passp_seria', $this->validation->passp_seria?$this->validation->passp_seria:$content['DS']->passp_seria);
			# } passp_seria
			
			# { passp_num
			$content['passp_num'] = $this->RenderLabel('passp_num');
			$content['passp_num_field'] =  form_input('passp_num', $this->validation->passp_num?$this->validation->passp_num:$content['DS']->passp_num);
			# } passp_num
			
			# { fpassp_seria
			$content['fpassp_seria'] = $this->RenderLabel('fpassp_seria');
			$content['fpassp_seria_field'] =  form_input('fpassp_seria', $this->validation->fpassp_seria?$this->validation->fpassp_seria:$content['DS']->fpassp_seria);
			# } fpassp_seria
			
			# { fpassp_num
			$content['fpassp_num'] = $this->RenderLabel('fpassp_num');
			$content['fpassp_num_field'] =  form_input('fpassp_num', $this->validation->fpassp_num?$this->validation->fpassp_num:$content['DS']->fpassp_num);
			# } fpassp_num
			
			# { stazh
			$content['stazh'] = $this->RenderLabel('stazh');
			$content['stazh_field'] =  form_input('stazh', $this->validation->stazh?$this->validation->stazh:$content['DS']->stazh);
			# } stazh
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$content['email_field'] =  form_input('email', $this->validation->email?$this->validation->email:$content['DS']->email);
			# } email
			
			# { nal_number
			$content['nal_number'] = $this->RenderLabel('nal_number');
			$content['nal_number_field'] =  form_input('nal_number', $this->validation->nal_number?$this->validation->nal_number:$content['DS']->nal_number);
			# } nal_number
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('employeers/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->employeers_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
}

?>